package com.airclick.merchant.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 说明：日期处理
 * 创建人：zsj
 * 修改时间：2017年11月24日
 * @version
 */
public class DateUtil {

	public final static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
	public final static SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");
	public final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public final static SimpleDateFormat sdfTimes = new SimpleDateFormat("yyyyMMddHHmmss");
	public final static SimpleDateFormat sdfDayss = new SimpleDateFormat("yyyyMM");
	public final static SimpleDateFormat sdfMonthDay = new SimpleDateFormat("MMdd");

	/**
	 * 获取yyyyMMddHHmmss格式
	 * @return
	 */
	public static String getSdfTimes() {
		return sdfTimes.format(new Date());
	}

	/**
	 * 获取yyyyMM格式
	 * @return
	 */
	public static String getSdfTimess() {
		return sdfDayss.format(new Date());
	}

	/**
	 * 获取YYYY-MM-DD HH:mm:ss格式
	 * @return
	 */
	public static String getTime() {
		return sdfTime.format(new Date());
	}

	public static boolean compareDate(Date s, Date e) {
		if(fomatDateToDate(s,sdfDay)==null||fomatDateToDate(e,sdfDay)==null){
			return false;
		}
		return fomatDateToDate(s,sdfDay).getTime() >fomatDateToDate(e,sdfDay).getTime();
	}

	/**
	 * 格式化日期
	 * @return
	 */
	public static Date fomatDate(String date) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			if(date==null || "".equals(date) || "null".equals(date))
				return null;

			return fmt.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 格式化日期
	 * @return
	 */
	public static Date fomatDate(String date,SimpleDateFormat sdfmt) {
		try {
			if(date==null || "".equals(date) || "null".equals(date))
				return null;

			return sdfmt.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}


	/**
	 * 格式化日期
	 * @return
	 */
	public static String fomatDate(Date date,SimpleDateFormat sdfmt) {

		try {
			if(date==null )
				return null;

			return sdfmt.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 格式化日期
	 * @return
	 */
	public static Date fomatDateToDate(Date date,SimpleDateFormat sdfmt) {
		return fomatDate(fomatDate(date,sdfmt),sdfmt);
	}

    /**
     * @param days
     * @return
     */
    public static Date getTimeOf0(int days) {
    	 Calendar cal = Calendar.getInstance();
         cal.setTime(new Date());
         cal.set(Calendar.HOUR_OF_DAY, 0);
         cal.set(Calendar.MINUTE, 0);
         cal.set(Calendar.SECOND, 0);
         cal.set(Calendar.MILLISECOND, 0);
         cal.add(Calendar.DAY_OF_MONTH, days);
         Date date = cal.getTime();
         return date;
	}

	/***
	 * 判断字符串是否是yyyy-MM-dd格式
	 * @param mes 字符串
	 * @return boolean 是否是日期格式
	 */
	public static boolean isRqFormat(String mes) {
		String format = "([0-9]{4})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])";
		Pattern pattern = Pattern.compile(format);
		Matcher matcher = pattern.matcher(mes);
		if (matcher.matches()) {
			pattern = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})");
			matcher = pattern.matcher(mes);
			if (matcher.matches()) {
				int y = Integer.valueOf(matcher.group(1));
				int m = Integer.valueOf(matcher.group(2));
				int d = Integer.valueOf(matcher.group(3));
				if (d > 28) {
					Calendar c = Calendar.getInstance();
					c.set(y, m - 1, 1);
					//每个月的最大天数
					int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
					return (lastDay >= d);
				}
			}
			return true;
		}
		return false;
	}

	// 根据日期获取一个月的时间
	public static List<String> getDayByMonth(int yearParam, int monthParam) {
		List list = new ArrayList();
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String str = sdf.format(d);
		int nowYear = Integer.parseInt(str.split("-")[0]);//年份
		int nowMonth = Integer.parseInt(str.split("-")[1]);//月份
		int nowDay = Integer.parseInt(str.split("-")[2]);
		Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
		aCalendar.set(yearParam, monthParam, 1);
		int year = aCalendar.get(Calendar.YEAR);//年份
		int month = aCalendar.get(Calendar.MONTH) + 1;//月份
		int day = aCalendar.getActualMaximum(Calendar.DATE);
		for (int i = day; i >= 1; i--) {
			String aDate = null;
			if (nowYear < year) {
				continue;
			}
			if (nowYear == year && nowMonth < month) {
				continue;
			}
			if (nowYear == year && nowMonth == month && nowDay < i) {
				continue;
			}
			if (month < 10 && i < 10) {
				aDate = String.valueOf(year) + "-0" + month + "-0" + i;
			}
			if (month < 10 && i >= 10) {
				aDate = String.valueOf(year) + "-0" + month + "-" + i;
			}
			if (month >= 10 && i < 10) {
				aDate = String.valueOf(year) + "-" + month + "-0" + i;
			}
			if (month >= 10 && i >= 10) {
				aDate = String.valueOf(year) + "-" + month + "-" + i;
			}
			list.add(aDate);
		}
		return list;
	}

	// 根据日期获取一个月的时间
	public static List<String> getMonthByYear(int yearParam) {
		List list = new ArrayList();
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		String str = sdf.format(d);
		int nowYear = Integer.parseInt(str.split("-")[0]);//年份
		int nowMonth = Integer.parseInt(str.split("-")[1]);//月份
		int months = 0;
		if(nowYear == yearParam) {//当年，按当前月计算
			months = nowMonth;
		} else if(nowYear > yearParam) {//非当年，有12个月
			months = 12;
		}
		for (int i = 1; i < months + 1; i++) {
			if(i<10) {
				list.add(yearParam + "-0" + i);
			} else {
				list.add(yearParam + "-" + i);
			}
		}
		return list;
	}

	/**
	 * 格式化日期
	 * @return
	 */
	public static String monthChange(String date) {
		String[] dates = date.split("-");
		String year = dates[0];
		String month = dates[1];
		StringBuffer sb = new StringBuffer();
		sb.append(year).append("年");
		if(Integer.parseInt(month) < 10) {
			sb.append(Integer.parseInt(month));
		} else {
			sb.append(month);
		}
		sb.append("月");
		return sb.toString();
	}

}
